package edu.uwlax.cs.oayonlinestore.client.gui.shared;

import java.util.Iterator;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import edu.uwlax.cs.oayonlinestore.client.OnlinestoreRService;
import edu.uwlax.cs.oayonlinestore.client.utils.Currency;
import edu.uwlax.cs.oayonlinestore.client.utils.DefaultAsyncCallback;
import edu.uwlax.cs.oayonlinestore.client.utils.HSpacer;
import edu.uwlax.cs.oayonlinestore.client.utils.Table;
import edu.uwlax.cs.oayonlinestore.vo.GiftCardVO;
import edu.uwlax.cs.oayonlinestore.vo.OrderEditVO;
import edu.uwlax.cs.oayonlinestore.vo.OrderExtendedVO;
import edu.uwlax.cs.oayonlinestore.vo.OrderItemVO;

public class OrderSummary extends VerticalPanel {

	private class OrderEditHandler extends DefaultAsyncCallback {
		public void onSuccess(Object arg0) {
			setOrderEdit((OrderEditVO) arg0);
		}
	}

	private class OrderExtendedHandler extends DefaultAsyncCallback {
		public void onSuccess(Object arg0) {
			setOrderExtended((OrderExtendedVO) arg0);
		}
	}

	private class ProductList extends Table {

		public ProductList() {
			super();
		}

		public void addGiftCard(float price) {
			AddRow r = addRow();
			r.add("Gift Card:").setColspan(2).setHAlign(r.RIGHT);
			r.add(new Currency(-price));
		}

		public void addItem(OrderItemVO o) {
			AddRow r = addRow();
			r.add(o.getQuantity() + "x " + o.getProduct().getBrand() + " "
					+ o.getProduct().getName() + ": "
					+ Currency.format(o.getPrice()));

			r.add("- " + o.getDiscount() + "% discount").setHAlign(r.RIGHT);
			r.add("= " + Currency.format(o.getTotal())).setHAlign(r.RIGHT);
		}

		public void addShipping(float price) {
			AddRow r = addRow();
			r.add("Shipping:").setColspan(2).setHAlign(r.RIGHT);
			r.add(new Currency(price));
		}

		public void addTax(float percent, float price) {
			AddRow r = addRow();
			r.add("Tax (" + percent + "%):").setColspan(2).setHAlign(r.RIGHT);
			r.add(new Currency(price));
		}

		public void addTotal(float val) {
			AddRow r = addRow();
			r.add("Total:").setColspan(2).setHAlign(r.RIGHT);
			r.add(new Currency(val)).bold();
		}

	}

	private HTML billing;

	private HTML customer;

	private HTML date;

	private HTML orderID;

	private HTML payment;

	private HTML shipping;

	private ProductList table;

	public OrderSummary() {
		super();
		add(orderID = new HTML());
		add(date = new HTML());
		add(customer = new HTML());
		add(payment = new HTML());
		add(new HSpacer());
		add(new HTML("<b>Selected Products</b>"));
		add(table = new ProductList());
		add(new HSpacer());
		add(new HTML("<b>Shipping and Payment Information</b>"));
		add(createDeleveryAndPaymentInfo());
		add(new HSpacer());
		add(createStoreInfo());
	}

	private Widget createDeleveryAndPaymentInfo() {
		VerticalPanel panel = new VerticalPanel();
		panel.add(shipping = new HTML());
		panel.add(billing = new HTML());
		return panel;
	}

	private Widget createStoreInfo() {
		VerticalPanel panel = new VerticalPanel();
		panel.add(new HTML("<b>QAY Online Store</b>"));
		panel.add(new HTML("17th Street 124, La Crosse, Wi 45345"));
		return panel;
	}

	protected void setOrderEdit(OrderEditVO oe) {
		shipping.setHTML("Shipping Address: " + oe.getShipStreetAddr() + ", "
				+ oe.getShipCity() + ", " + oe.getShipState() + " "
				+ oe.getShipZipCode());
		billing.setHTML("Billing Address: " + oe.getBillStreetAddr() + ", "
				+ oe.getBillCity() + ", " + oe.getBillState() + " "
				+ oe.getBillZipCode());
		payment.setHTML("Payment: Credit Card (" + oe.getCcCompany() + ")");
	}

	protected void setOrderExtended(OrderExtendedVO oe) {
		orderID.setHTML("OrderID: <b>" + oe.getId() + "</b>");
		if (oe.getOrderDate() != null)
			date.setHTML("Date: " + oe.getOrderDate());
		else
			date.setHTML("");
		customer.setHTML("Customer: " + oe.getUser().getFirstname() + " "
				+ oe.getUser().getLastname());

		table.truncateToSize(0);
		for (Iterator i = oe.getOrderItems().iterator(); i.hasNext();)
			table.addItem((OrderItemVO) i.next());
		for (Iterator i = oe.getGiftCards().iterator(); i.hasNext();)
			table.addGiftCard(((GiftCardVO) i.next()).getMoneyValue());
		table.addShipping(oe.getShipping());
		table.addTax(oe.getTaxPercent(), oe.getTaxMoney());
		table.addTotal(oe.getTotal());
	}

	public void update(int orderID) {
		OnlinestoreRService.Util.getInstance().getOrderExtended(orderID,
				new OrderExtendedHandler());
		OnlinestoreRService.Util.getInstance().getOrderEdit(orderID,
				new OrderEditHandler());
	}

}
